<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>登录</title>
    {% include "../header.html" %}
</head>

<body>
    <center>
        <div class="card" style="width: 400px;margin-top: 10px">
            <div class="card-body">
                <h4>登录</h4>
                <form id="reg" role="form" onSubmit="return checkreg();">
                    {% csrf_token %}
                    <table class="table">
                        <tr>
                            <td>用户名</td>
                            <td><input type="text" name="username" class="form-control"></td>
                        </tr>
                        <tr>
                            <td>密码</td>
                            <td><input type="password" name="password" class="form-control"></td>
                        </tr>
                    </table>
                    <hr />
                    <button class="btn btn-success">提交</button>
                </form>
            </div>
        </div>
    </center>
    {% include "../footer.html" %}
    <script>
        function checkreg(e) {
            if ($("input[name=password]").val() == "") {
                swal({
                    title: "Oops",
                    text: '请填写密码',
                    icon: "error",
                    button: "重新输入",
                });
                return false;
            }
            if ($("input[name=user_id]").val() == "") {
                swal({
                    title: "Oops",
                    text: '请填写用户名',
                    icon: "error",
                    button: "重新输入",
                });
                return false;
            }
            $.ajax({
                type: 'POST',
                url: "{% url 'vt1:apiLogin' %}",
                data: $("#reg").serialize(), 
                success: function (data) {
                    ret = JSON.parse(data);
                    if (ret['code'] == 1) {//error
                        swal({
                            title: "Oops",
                            text: ret['msg'],
                            icon: "error",
                            button: "重新输入",
                        }).then((value) => {
                            $("input[name=password]").val("");
                        });
                        return false;
                    }
                    else {
                        swal({
                            title: "OK",
                            text: ret['msg'],
                            icon: "success",
                            button: "确定",
                        }).then((value) => {
                            window.location.href = "{% url 'vt1:indexPage' %}";
                        });
                        return false;
                    }
                }
            });
            return false;
        }
    </script>
</body>

</html>